I use the following code to do a string replace for certain characters in a buffer to make it XML compliant: |
Re: Is there a way to make this simpler? You should have used the {co de} ... {co de} statement (without space) in your post to put the code in a readable block inside your post. Some stuff seems to be lost, however from what I can see you function is duplicating characters in a Buffer? Declaring a special Buffer outside the function is indeed good practice in DXL. There is nothing wrong with it. Since DXL is single threaded you will never have the problem of the buffer being used by different threads. You only need one allocation. Your code is as readable as if you declared the buffer inside the function (in fact the declaration is 3-4 lines above where it would be inside the function), so nothing wrong with a tempbuffer here. I just would name it something like gTempBufConvertXMLSymbols then you do not need to fear some other gui declaring a tempbuffer in his code and the two libraries fighting against each other. If you want to discuss details about optimizations to this code, just post ... Maybe that helps, Regards, Mathias Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: Is there a way to make this simpler? Mathias Mamsch - Fri Nov 11 14:30:35 EST 2011 Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
I'm working with some old code dating back a few years. What I wanted to do was rewrite the function without having to declare the buffer outside the function. I wanted to keep it all self-contained and return a string.
string convertXmlSymbols_(Buffer b) {
int i = 0
for (i=0; i <= length(b); i++) {
// Strip any unnecessary control characters (defined in ASCII decimal range of 0-31).
if (iscntrl(b[i]) && !isspace(b[i])) continue
// Replace XML control characters with XML entity.
if (b[i:i] == "'") {
if (i == 0) {
// If ' appears at the beginning of the buffer.
b = replaceRichText(tempStringOf(b), i, 1, "'")
} else if (!(b[i-1:i-1] == "\\")) {
// If ' appears without a \ in front of it (denotes RTF control character.)
b = replaceRichText(tempStringOf(b), i, 1, "'")
}
} else if (b[i] == '<') {
b = replaceRichText(tempStringOf(b), i, 1, "<")
} else if (b[i] == '>') {
b = replaceRichText(tempStringOf(b), i, 1, ">")
} else if (b[i] == '"') {
b = replaceRichText(tempStringOf(b), i, 1, """)
} else if (b[i] == '&') {
b = replaceRichText(tempStringOf(b), i, 1, "&")
}
}
return tempStringOf(b)
}
|
Re: Is there a way to make this simpler? SystemAdmin - Fri Nov 11 15:26:58 EST 2011
I'm working with some old code dating back a few years. What I wanted to do was rewrite the function without having to declare the buffer outside the function. I wanted to keep it all self-contained and return a string.
string convertXmlSymbols_(Buffer b) {
int i = 0
for (i=0; i <= length(b); i++) {
// Strip any unnecessary control characters (defined in ASCII decimal range of 0-31).
if (iscntrl(b[i]) && !isspace(b[i])) continue
// Replace XML control characters with XML entity.
if (b[i:i] == "'") {
if (i == 0) {
// If ' appears at the beginning of the buffer.
b = replaceRichText(tempStringOf(b), i, 1, "'")
} else if (!(b[i-1:i-1] == "\\")) {
// If ' appears without a \ in front of it (denotes RTF control character.)
b = replaceRichText(tempStringOf(b), i, 1, "'")
}
} else if (b[i] == '<') {
b = replaceRichText(tempStringOf(b), i, 1, "<")
} else if (b[i] == '>') {
b = replaceRichText(tempStringOf(b), i, 1, ">")
} else if (b[i] == '"') {
b = replaceRichText(tempStringOf(b), i, 1, """)
} else if (b[i] == '&') {
b = replaceRichText(tempStringOf(b), i, 1, "&")
}
}
return tempStringOf(b)
}
|
Re: Is there a way to make this simpler? SystemAdmin - Fri Nov 11 15:26:58 EST 2011
I'm working with some old code dating back a few years. What I wanted to do was rewrite the function without having to declare the buffer outside the function. I wanted to keep it all self-contained and return a string.
string convertXmlSymbols_(Buffer b) {
int i = 0
for (i=0; i <= length(b); i++) {
// Strip any unnecessary control characters (defined in ASCII decimal range of 0-31).
if (iscntrl(b[i]) && !isspace(b[i])) continue
// Replace XML control characters with XML entity.
if (b[i:i] == "'") {
if (i == 0) {
// If ' appears at the beginning of the buffer.
b = replaceRichText(tempStringOf(b), i, 1, "'")
} else if (!(b[i-1:i-1] == "\\")) {
// If ' appears without a \ in front of it (denotes RTF control character.)
b = replaceRichText(tempStringOf(b), i, 1, "'")
}
} else if (b[i] == '<') {
b = replaceRichText(tempStringOf(b), i, 1, "<")
} else if (b[i] == '>') {
b = replaceRichText(tempStringOf(b), i, 1, ">")
} else if (b[i] == '"') {
b = replaceRichText(tempStringOf(b), i, 1, """)
} else if (b[i] == '&') {
b = replaceRichText(tempStringOf(b), i, 1, "&")
}
}
return tempStringOf(b)
}
The code you have posted did not look like valid DXL. I was totally confused that you seem to replace a character by itself, until I realized the forum has replaced the XML tags to valid characters while parsing your post. :-) Anyway, the fastest way for replacing a bunch of expressions in a large string is a regular expression (that has to be global) which will find the position of your expressions very quickly in large strings. For strings with a low size (about < 1000 chars) it is much faster to iterate over them using the character by character approach. In both cases you will build up a new buffer and either allocate and return it (compare the times of replacing the contents of the old buffer by the time for the allocation!) or replace the contents of the old buffer. Okay, now coming to the specific problems with that second approach:
Now coming to self-contained-ness ... As I said, in DXL the best thing you can do is to use a global buffer that is used only in one function. You cannot avoid globals in DXL, so better learn to live with them ;-) It can really be considered good practice to do so, so for whatever reason you want self contained functions, you might want to rethink. If you really want a self contained function at all costs, then let the user pass two buffers to the function. One input buffer, one output buffer. Rebuilding the string to a new buffer is essential for performance in that algorithm, you cannot get around this without serious memory or performance issues. Maybe that gives you some insights into performance optimization in DXL. Regards, Mathias Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: Is there a way to make this simpler? You can search for reference within this forum. |
Re: Is there a way to make this simpler? OurGuest - Mon Nov 14 09:57:03 EST 2011 |